﻿using System;
using System.Collections.Generic;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCA.UI.Utils;
using Telerik.Windows.Controls.Charting;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCA.UI.Controls;
using System.Reflection;
using Baosight.ZNH.ZMCA.UI.Model;
using System.Windows.Controls;
using System.Windows;
using Baosight.ZNH.ZMCA.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;


namespace Baosight.ZNH.ZMCA.UI
{


    public partial class ZMCA19 : EFView
    {
        ZMCA03Controler mineControler = new ZMCA03Controler();
        ZMCA03Controler grubControler = new ZMCA03Controler();
        public ZMCA19()
        {
            InitializeComponent();
            grubControler.PlanType = "掘进计划";
            mineControler.ShowChartYearDataAndTrendLine = false;
            grubControler.ShowChartYearDataAndTrendLine = false;
            mineControler.UpdateChartDateCompeleted += new ZMCA03Controler.UpdateChartDateCompeleteHandler(mineControler_UpdateChartDateCompeleted);
            grubControler.UpdateChartDateCompeleted += new ZMCA03Controler.UpdateChartDateCompeleteHandler(grubControler_UpdateChartDateCompeleted);
        }

        void mineControler_UpdateChartDateCompeleted(object sender, List<ChartData> e)
        {
            lstViewMine.ItemsSource = e;
            decimal planTotal = 0;
            decimal actualTotal = 0;
            for (int i = 0; i < e.Count; i++)
            {
                planTotal = planTotal + e[i].PlanValue;
                actualTotal = actualTotal + e[i].DataValue;
            }

            fdMinePlan.Text = planTotal.ToString("N2")+"吨";
            fdMineActual.Text = actualTotal.ToString("N2") + "吨";
            fdMineRate.Text = Common.GetPercentText(actualTotal, planTotal);

            chtMine.DefaultView.ChartTitle.Content = "采煤量月对比";
            grubControler.GetChartData(chtGrub);
        }

        void grubControler_UpdateChartDateCompeleted(object sender, List<ChartData> e)
        {
            this.IsBusy = false;
            chtGrub.DefaultView.ChartTitle.Content = "掘进量月对比";
            lstViewGrub.ItemsSource = e;
            decimal planTotal = 0;
            decimal actualTotal = 0;
            for (int i = 0; i < e.Count; i++)
            {
                planTotal = planTotal + e[i].PlanValue;
                actualTotal = actualTotal + e[i].DataValue;
            }

            fdGrubPlan.Text = planTotal.ToString("N2") + "米";
            fdGrubActual.Text = actualTotal.ToString("N2") + "米";
            fdGrubRate.Text = Common.GetPercentText(actualTotal, planTotal);
        }

        public override void Initialize(Platform.Core.EIInfo initData)
        {
            base.Initialize(initData);
            ZMCA03Controler.InitWarningPassRate(initData);
            InitCbYear();

            FillData();
        }

        /// <summary>
        /// 初始化年选择列表
        /// </summary>
        private void InitCbYear()
        {
            int currentYear = DateTime.Now.Year;
            for (int i = 0; i < 3; i++)
            {
                cbYear.Items.Add(currentYear - i);
            }
            cbYear.SelectedIndex = 0;
            cbYear.SelectionChanged+=new Telerik.Windows.Controls.SelectionChangedEventHandler(cbYear_SelectionChanged);
        }

        void cbYear_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
        {
            FillData();
        }

        /// <summary>
        /// 获取数据并绑定到界面上
        /// </summary>
        public void FillData()
        {
            mineControler.PlanYear = cbYear.SelectedItem.ToString();
            grubControler.PlanYear = cbYear.SelectedItem.ToString();
            mineControler.GetChartData(chtMine);
            GetOtherProjectCount();
            this.IsBusy = true;
        }

        /// <summary>
        /// 获取其他工程数据
        /// 其他工程为采煤工程和掘进工程之外的工程
        /// </summary>
        private void GetOtherProjectCount()
        {
            int year = Common.GetIntValue(cbYear.SelectedItem);
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA03");
            eiInfo.Set("method", "queryOtherProjectCount");
            eiInfo.Set("fdYear", year);
            this.Submit(eiInfo, loadOtherProjectCountCompleted);
        }

        private void loadOtherProjectCountCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                return;
            }

            EIBlock resultBlock = e.EIInfo.GetBlock("result");
            EIRow row = resultBlock.GetRow(0);

            decimal planTotal = Common.GetDecimalValue(row["fdPlanMine"]);
            decimal actualTotal = Common.GetDecimalValue(row["fdActualSum"]);

            fdOtherPlan.Text = planTotal.ToString("N0") + "个";
            fdOtherActual.Text = actualTotal.ToString("N0") + "个";
            fdOtherRate.Text = Common.GetPercentText(actualTotal, planTotal);
        }

        /// <summary>
        /// 查看月明细数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;

            ChartData cd = btn.DataContext as ChartData;
            CmesWindows window1 = new CmesWindows();
            window1.Header = cd.Date + "生产计划";
            ProducePlanMonthDetailView view = new ProducePlanMonthDetailView();
            string planType = "采煤计划";
            if ((lstViewGrub.ItemsSource as List<ChartData>).Contains(cd))
            {
                planType = "掘进计划";
            }
            int year = Common.GetIntValue(cbYear.SelectedItem);
            int month = Common.GetIntValue(cd.Date.Substring(0, cd.Date.Length - 1));
            view.LoadData(year, month, planType);
            window1.Content = view;
            window1.ShowDialog();
        }
    }
}